﻿/***************************************************************************************
   Date    : February 7, 2007
   Purpose : Implement the Utility Class
           : DefaultDisplayDuration, ScreenScene, BatsmanStatus, BowlerStatus & Boundary Enumerations
   Maintenance Log : 
   Revision		Date			Description 
   --------		---------		-----------------
	1.0		    7/02/2007		Initial Release

****************************************************************************************/

using System;
using System.Configuration;
using System.Web;

namespace Hclt.Msft.Til.CricketScoreboard
{
    #region Public Enumerations

    /// <summary>
    /// Enum To Describe The Default Display Time For Different Scenes.
    /// </summary>
    public enum DefaultDisplayDuration
    {
        None=0,
        FourHitScene = 5,
        SixHitScene = 5,
        OutScene = 10,
        BowlingScene = 20,
        BattingScene = 40
    }

    /// <summary>
    /// Enum To Describe The Different Scenes That Can Occur.
    /// </summary>
    public enum ScreenScene
    {
        FourHit,
        SixHit,
        BatsmanOut,
        BowlingSheet,
        BattingSheet,
        InningsOver,
        None
    }

    /// <summary>
    /// Enum To Describe The Different Status Of A Batsman.
    /// </summary>
    public enum BatsmanStatus
    {
        Dnb,
        Playing,
        Out,
        Retired,        
    }

    /// <summary>
    /// Enum To Describe The Different Status Of A Bowler.
    /// </summary>
    public enum BowlerStatus
    {
        Rest,
        Bowling,     
    }

    ///<summary>
    /// Enum To Describe The Different Score Assosiated With Different Boundaries.
    /// </summary>
    public enum Boundary
    {
        Four = 4,
        Six = 6,
        None = 0
    }

    /// <summary>
    /// Enum To Describe The Different Innings In A Match.
    /// </summary>
    public enum Innings
    {
        First,
        Second
    }

    #endregion
    
    /// <summary>
    /// Contains The Different Utility Functions That Are Used In Whole Application.
    /// </summary>
    public static class Utility
    {
        #region Public Static Functions

        /// <summary>
        /// Returns A String Value Of The Supplied Node After Reading From The "web.config" 
        /// </summary>
        /// <param name="key">Represents The Node ID Placed Inside The "web.config" File</param>
        /// <param name="defaultValue">Gives The Default Value In Case Node Is Not Present in "web.config"</param>
        /// <returns>Returns The Value Associated With The Node</returns>
        public static string GetStringFromConfig(string key, string defaultValue)
        {
            string returnValue;
            returnValue = ConfigurationManager.AppSettings.Get(key);
            if (string.IsNullOrEmpty(returnValue)) returnValue = defaultValue;
            return returnValue;
        }

        /// <summary>
        /// Returns An Integer Value Of The Supplied Node After Reading From The "web.config" 
        /// </summary>
        /// <param name="key">Represents The Node ID Placed Inside The "web.config" File</param>
        /// <param name="defaultValue">Gives The Default Value In Case Node Is Not Present in "web.config"</param>
        /// <returns>Returns The Value Associated With The Node</returns>
        public static int GetIntFromConfig(string key, int defaultValue)
        {
            string keyValue;
            keyValue = ConfigurationManager.AppSettings.Get(key);
            Int32.TryParse(keyValue, out defaultValue);
            return defaultValue;
        }
        
        /// <summary>
        /// Inserts A Key Value Pair Into The Session.
        /// </summary>
        /// <param name="key">The Key That Is To Be Inserted</param>
        /// <param name="value">The Value To Be Associated With The Key</param>
        public static void AddToSession(string key, object value)
        {
            HttpContext.Current.Session.Add(key, value);            
        }
        
        /// <summary>
        /// Reyurns The Value Associated With The Supplied Key From The Session.
        /// </summary>
        /// <param name="key">Key To Be Recovered From The Session.</param>
        /// <returns>Value Object Associated With The Key.</returns>
        public static object GetFromSession(string key)
        {
            return HttpContext.Current.Session[key];
        }

        /// <summary>
        /// Returns The TimeSpan Of The Currently Displaying Scene.
        /// </summary>
        /// <param name="scene">Scene For Which The TimeSpan Is Required.</param>
        /// <returns>TimeSpan Of The Supplied Scene.</returns>
        public static TimeSpan GetSceneDisplayTime(ScreenScene scene)
        {
            TimeSpan displayTime = new TimeSpan(0, 0, 0);
            ScreenScene currentScene = GetCurrentScene();
            if (currentScene == scene)
            {
                object sessionSceneTime = GetFromSession("CurrentSceneTime");
                if(sessionSceneTime!=null)
                {
                    DateTime currentSceneTime = (DateTime)sessionSceneTime;
                    displayTime = DateTime.Now.Subtract(currentSceneTime);
                }
            }
            return displayTime;
        }

        /// <summary>
        /// Inserts The Scene & The Start Time Of It If It Is not The Current Scene In Session.
        /// </summary>
        /// <param name="scene">The Scene To Be Inserted</param>
        public static void AddSceneToSession(ScreenScene scene)
        {
            ScreenScene currentScene = GetCurrentScene();
            if (currentScene != scene)
            {
                AddToSession("CurrentScene", scene);
                AddToSession("CurrentSceneTime", DateTime.Now);
            }
        }

        /// <summary>
        /// Returns The Current Scene Displaying On The Screen From The Session.
        /// </summary>
        /// <returns>ScreenScene Enum Object</returns>
        public static ScreenScene GetCurrentScene()
        {
            object sessionScene = GetFromSession("CurrentScene");
            ScreenScene currentScene = ScreenScene.None;
            if (sessionScene != null)
            {
                currentScene = (ScreenScene)sessionScene;
            }
            return currentScene;
        }

        #endregion
    }
}